<template>
  <div id="app">
    <transition name="slide">
      <Toast v-show="isShowToast" />
    </transition>
    <Topbar :key="topbarVal" />
    <Header />
    <router-view @updateKey="updateKey" />
    <Footer />
    <Login v-show="isShowModal" />
  </div>
</template>
<script>
import Topbar from "@/components/Topbar.vue";
import Header from "@/components/Header.vue";
import Footer from "@/components/Footer.vue";
import Login from "@/components/Login.vue";
import Toast from "@/components/Toast.vue";

import { recommend } from "@/request/api";

import { mapState, mapMutations } from "vuex";
export default {
  data() {
    return {
      topbarVal: 1,
    };
  },
  components: {
    Topbar,
    Header,
    Footer,
    Login,
    Toast,
  },
  computed: {
    ...mapState({
      isShowModal: (state) => state.showModal.isShowModal,
      isShowToast: (state) => state.showToast.isShowToast,
    }),
  },
  watch: {
    // 切换路由更新登录状态:方式二:组件重载
    "$route.path": {
      handler(newVal, oldVal) {
        if (newVal != oldVal) {
          this.topbarVal++;
        }
      },
    },
  },
  methods: {
    ...mapMutations({
      changeIsShowToast: "showToast/changeIsShowToast",
      chanIsShowLoginModal: "showModal/chanIsShowLoginModal",
    }),
    showToastFn() {
      this.chanIsShowToast(true);
      setTimeout(() => {
        this.chanIsShowToast(false);
      }, 1500);
    },
    updateKey() {
      this.topbarVal++;
    },
  },

  // 页面加载之前提取数据
  // 异步函数async-->await====>解决回调地狱

  created() {
    recommend()
      .then((res) => {
        // console.log("请求成功：", res);
      })
      .catch((err) => {
        // console.log("请求失败：", err);
      });
  },
};
</script>

<style lang="less">
// #app {
//   font-family: Avenir, Helvetica, Arial, sans-serif;
//   -webkit-font-smoothing: antialiased;
//   -moz-osx-font-smoothing: grayscale;
//   text-align: center;
//   color: #2c3e50;
// }

/* 入场的起始状态 = 离场的结束状态 */
.slide-enter,
.slide-leave-to {
  opacity: 0;
}

.slide-enter-active,
.slide-leave-active {
  transition: opacity 0.5s linear;
}

.slide-enter-to,
.slide-leave {
  opacity: 1;
}
</style>
